/**
 * Created by novem on 15/9/25.
 */
var storage = window.localStorage;
window.onload=function(){
    var logId = getUserId();
    var url = getUrl();
    var photobookId = $.getUrlParam('photobookId');
    var spotsId = $.getUrlParam("spotsId");
    storage.setItem("spotsId",spotsId);
    $.jsonp({
        url: 'http://'+url+'/api/photobook/detail-'+photobookId,
        data: {},
        callbackParameter: "callback",
        success: function (data, textStatus, xOptions) {
            $("#jb").attr("href","jubao.html?type=1&objId="+data.user.id);
            $("#chat").attr("onclick","easeMobGoChat("+data.user.telphone+")");
            $("#comment").attr("href","xiedianping.html?id="+photobookId+"&param=photobook");
            var userId = data.user.id;
            data.photobookId =photobookId;
            $("#user").text(userId);
            var commentSize = data.commentSize;
            $("#commentSize").text(commentSize);
            var lookNumber = data.lookNumber;
            $("#lookNumber").text(lookNumber);
            var greatNumber = data.greatNumber;
            $("#greatNumber").text(greatNumber);
            var complied = juicer($("#top_tpl").text());
            var html = complied.render(data);
            $(".ui-user-follow-info").append(html);
            var complied01 = juicer($("#photo_tpl").text());
            var html01 = complied01.render(data);
            $(".photo-list").append(html01);
            var complied04 = juicer($("#bottom_tpl").text());
            var html04 = complied04.render(data);
            $(".album-bottom-menu").append(html04);
            $(".edit").attr('data-gz',data.user.id);

            //添加查看次数
            $.jsonp({
                url: 'http://'+url+'/api/photobook/look_photobook-'+photobookId,
                data: {photobookId:photobookId,userId:logId},
                callbackParameter: "callback",
                success: function (data, textStatus, xOptions) {},
                error: function (xOptions, textStatus) {}
            });
            //判断关注与否
            $.jsonp({
                url: 'http://'+url+'/api/user/user_cluck_list',
                data: {userId:logId},
                callbackParameter: "callback",
                success: function (data, textStatus, xOptions) {
                    $.each(data.arrays,function(index,ele){
                        var cluckId = ele.cluckId;
                        if(cluckId==userId){
                            $(".edit").text("已关注").css({color: '#c5c5c5', 'border': '1px solid #c5c5c5'});
                        }
                    })
                },
                error: function (xOptions, textStatus) {}
            });
            //判断收藏与否
            $.jsonp({
                url: 'http://'+url+'/api/collect/collect_photobook_list',
                data: {userId:logId},
                callbackParameter: "callback",
                success: function (data, textStatus, xOptions) {
                    $.each(data.arrays,function(index,ele){
                        var samewayId = ele.samewayId;
                        if(samewayId==photobookId){
                            $(".sc").addClass("btn").text("已收藏").css({'font-size':'14px','color':'#fff'});
                        }
                    })
                },
                error: function (xOptions, textStatus) {}
            });
            //判断点赞与否
            $.jsonp({
                url: 'http://'+url+'/api/photobook/bit_number-'+photobookId,
                data: {},
                callbackParameter: "callback",
                success: function (data, textStatus, xOptions) {
                    $.each(data.arrays,function(index,ele){
                        if(ele.id==logId){
                            $("#top_pic").attr("src","img/icons_zanh.png");
                            $("#bottom_pic").attr("src","img/album_bottom_menu_zans.png");
                        }
                    })
                },
                error: function (xOptions, textStatus) {}
            });
        },
        error: function (xOptions, textStatus) {}
    });

//看过人
    $(".ma_look").on("click",function(){
        $.jsonp({
            url: 'http://'+url+'/api/photobook/look_number-'+photobookId,
            data: {userId:logId},
            callbackParameter: "callback",
            success: function(data, textStatus, xOptions){
                var complied02 = juicer($("#order_tpl").text());
                var html02 = complied02.render(data);
                $(".order-list").html(html02);
            },
            error: function(xOptions, textStatus){}
        });
    });

//评论列表
    $(".ma_comment").on("click",function() {
        $.jsonp({
            url: 'http://'+url+'/api/photobook/comment_list-' + photobookId,
            data: {},
            callbackParameter: "callback",
            success: function (data, textStatus, xOptions) {
                $.each(data.arrays,function(index,ele) {
                    (function (){
                        var n = new Date().getTime();
                        o = n-ele.createTime*1000;
                        ele.createTime = ago(o);//返回距今时间
                        //传入时间毫秒 如果是时间戳 *1000
                    })();
                });
                var complied03 = juicer($("#comment_tpl").text());
                var html03 = complied03.render(data);
                $(".comment-list").html(html03);
            },
            error: function (xOptions, textStatus) {
            }
        });
    });

//关注
    $(document).on('click','.edit',function(){
        var userId=$(this).data('gz');
        var _txt=$(this).text();
        var obtn=this;
        if(userId!=logId) {
            $.jsonp({
                url: 'http://'+url+'/api/user/cluck_user-'+userId,
                data: {userId:logId},
                callbackParameter: "callback",
                success: function (data, textStatus, xOptions) {
                    var code = data.code;
                    if(code==1) {
                        $(obtn).removeClass("btn").text("已关注").css({color: '#c5c5c5', 'border': '1px solid #c5c5c5'});

                    }else{
                        $(obtn).addClass("btn").text("+关注").css({'color': '#00a5e0', width: '60px', height: '30px', 'padding': '0', 'border': '1px solid #00a5e0'});
                    }
                },
                error: function (xOptions, textStatus) {
                }
            });
        }else{
            showToast("不能自己关注自己哟~");
            return;
        }
    })

//点赞
    $(document).on('click','.wujiang_pic_size',function(){
        var obtn = this;
        if(this.getAttribute("src",2)=="img/icons_zan.png"){
            $.jsonp({
                url: 'http://'+url+'/api/photobook/bit_great-'+photobookId,
                data: {photobookId:photobookId,userId:logId},
                callbackParameter: "callback",
                success: function (data, textStatus, xOptions) {
                    obtn.src="img/icons_zanh.png";
                },
                error: function (xOptions, textStatus) {
                }
            });
        }
    })
    $(document).on('click','.wujiang_pic_sizea',function(){
        var obtn = this;
        if(this.getAttribute("src",2)=="img/album_bottom_menu_zan.png"){
            $.jsonp({
                url: 'http://'+url+'/api/photobook/bit_great-'+photobookId,
                data: {photobookId:photobookId,userId:logId},
                callbackParameter: "callback",
                success: function (data, textStatus, xOptions) {
                    obtn.src="img/album_bottom_menu_zans.png";
                },
                error: function (xOptions, textStatus) {
                }
            });
        }
    })
//点赞列表
    $(document).on('click','#zan-list',function(){

        $.jsonp({
            url: 'http://'+url+'/api/photobook/bit_number-'+photobookId,
            data: {},
            callbackParameter: "callback",
            success: function (data, textStatus, xOptions) {
                var complied04 = juicer($("#bit_tpl").text());
                var html04 = complied04.render(data);
                $("#bit_list").html(html04);
            },
            error: function (xOptions, textStatus) {}
        });
    })

//收藏
    $(document).on('click','.w_shac',function(){
        $.jsonp({
            url: 'http://'+url+'/api/collect/collect_photobook',
            data: {photobookId:photobookId,userId:logId},
            callbackParameter: "callback",
            success: function (data, textStatus, xOptions) {
                var code = data.code;
                if(code==1) {
                    $(".sc").removeClass("btn").text("收藏").css({'font-size':'14px','color':'#fff'});

                }else{
                    $(".sc").addClass("btn").text("已收藏").css({'font-size':'14px','color':'#fff'});
                }
            },
            error: function (xOptions, textStatus) {
            }
        });

    });
}

//function onBackKeyDown() {
//    var spotsId = storage.getItem(spotsId);
//    jumpbyback(3,"huace_jingdianhuace.html?spotsId="+spotsId);
//}

function ago(o){
    var times =o;
    var minute = 1000 * 60;
    var hour = minute * 60;
    var day = hour * 24;
    var month = day * 30;
    var year = month * 365;
    var yearC = times/year;
    var monthC =times/month;
    var dayC =times/day;
    var hourC =times/hour;
    var minC =times/minute;
    var html ="";
    if(yearC>=1){
        html += parseInt(yearC) + "年前";
        return html;
    }else if(monthC>=1){
        html += parseInt(monthC) + "月前";
        return html;
    }else if(dayC>=1){
        html += parseInt(dayC) + "天前";
        return html;
    }else if(hourC>=1){
        html += parseInt(hourC) + "个小时前";
        return html;
    }else if(minC>=1){
        html += parseInt(minC) + "分钟前";
        return html;
    }else{
        return "刚刚";
    }
}